<?php
require_once (dirname(__FILE__) .'/PtObjectAbstract.php');
class PtPreConfiguredDesign extends PtObjectAbstract
{
	public $id;
	public $id_cms;
	public $namespace;
	public $type;
	public $key_name;
	public $target_element;
	public $style;
	public $default;
	public $active;
	
	/**
	 * @see ObjectAbstract::$definition
	 */
	public static $definition = array(
		'table'   => 'preconfigured_design',
		'primary' => 'id_design',
		'fields'  => array(
			'id_cms'    => array('type' => self::TYPE_STRING, 'validate' => 'isUnsignedId', 'required' => true),
			'namespace' => array('type' => self::TYPE_STRING, 'validate' => 'isString', 'required' => true, 'size' => 256),
			'type'      => array('type' => self::TYPE_STRING, 'validate' => 'isString', 'required' => true),
			'key_name'  => array('type' => self::TYPE_STRING, 'validate' => 'isString', 'required' => true, 'size' => 64),
			'target_element' => array('type' => self::TYPE_STRING, 'validate' => 'isString', 'required' => true, 'size' => 256),
			'style'     => array('type' => self::TYPE_STRING, 'validate' => 'isString', 'required' => true),
			'default'    => array('type' => self::TYPE_BOOL,   'validate' => 'isBool'),
			'active'    => array('type' => self::TYPE_BOOL,   'validate' => 'isBool'),
		)
	);
	
	public static function getByIdCmsAndKeyName ($id_cms, $key_name)
	{
		if (!self::$db)
			self::$db = DbAbstract::getInstance();
		
		$sql = new PtDbQuery();
		$sql->from(self::$definition['table'], 'a');
		$sql->where('a.id_cms = '. (int)$id_cms .' AND a.key_name = \''. $key_name .'\' AND a.active = 1');
		
		$result = self::$db->getRow($sql, true, false);
		
		if ($result)
		{
			return new self($result[self::$definition['primary']]);
		}
		else
		{
			$item = new self();
			$item->id_cms   = $id_cms;
			$item->key_name = $key_name;
			return $item;
		}
	}
	
	public static function getByIdCmsAndNamespace ($id_cms, $namespace)
	{
		if (!self::$db)
			self::$db = DbAbstract::getInstance();
		
		$sql = new PtDbQuery();
		$sql->select('a.'. self::$definition['primary'])
			->from(self::$definition['table'], 'a')
			->where('a.id_cms = '. (int)$id_cms .' AND a.namespace LIKE \''. $namespace .'%\' AND a.active = 1');
		
		$result = self::$db->executeS($sql, true, false);
		
		if ($result)
		{
			$data = array();
			foreach ($result AS $item)
			{
				$data[] = new self($item[self::$definition['primary']]);
			}
			return $data;
		}
		else
		{
			return array();
		}
	}
	
	public static function getByIdCms ($id_cms, $default = false)
	{
		if (!self::$db)
			self::$db = DbAbstract::getInstance();
		
		$sql = new PtDbQuery();
		$sql->select('a.'. self::$definition['primary'])
			->from(self::$definition['table'], 'a')
			->where('a.id_cms = '. (int)$id_cms . ($default ? ' AND a.default = 1' : ''));
		
		$result = self::$db->executeS($sql, true, false);
		
		if ($result)
		{
			$data = array();
			foreach ($result AS $item)
			{
				$data[] = new self($item[self::$definition['primary']]);
			}
			return $data;
		}
		else
		{
			return array();
		}
	}
}